package com.jumi.microservice.rocketmq;

import com.aliyun.openservices.ons.api.MessageListener;
import com.aliyun.openservices.ons.api.PropertyKeyConst;
import com.aliyun.openservices.ons.api.bean.ConsumerBean;
import com.aliyun.openservices.ons.api.bean.Subscription;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.Map;
import java.util.Properties;

@Configuration
public class ConsumerClient {

    @Autowired
    private RocketMqConfig mqConfig;

    @Autowired
    private RocketMessageListener messageListener;

    @Resource
    private RocketMqOrderListener rocketMqOrderListener;

    @Resource
    private AutoSpiltOrderListener autoSpiltOrderListener;
    @Resource
    private UnPayOrderListener unPayOrderListener;
    @Resource
    private UnPayOrderTimeOutListener unPayOrderTimeOutListener;
    @Resource
    private InvoiceMessageListener invoiceMessageListener;

    @Bean(initMethod = "start", destroyMethod = "shutdown")
    public ConsumerBean buildOrderConsumer() {
        ConsumerBean orderConsumerBean = new ConsumerBean();
        //配置文件
        Properties properties = mqConfig.getMqProperties();
        properties.setProperty(PropertyKeyConst.GROUP_ID, mqConfig.getGroup());
        orderConsumerBean.setProperties(properties);
        //订阅order
        Map<Subscription, MessageListener> subscriptionTable = new HashMap<Subscription, MessageListener>();
        Subscription subscription = new Subscription();
        subscription.setTopic(mqConfig.getTopic());
        subscriptionTable.put(subscription, autoSpiltOrderListener);
        //订阅pay
        subscription = new Subscription();
        subscription.setTopic("pay");
        subscriptionTable.put(subscription, rocketMqOrderListener);
        //订阅unPay
        subscription = new Subscription();
        subscription.setTopic("unPay");
        subscriptionTable.put(subscription, unPayOrderListener);
        //订阅unPayTimeOut
        subscription = new Subscription();
        subscription.setTopic("unPayTimeOut");
        subscriptionTable.put(subscription, unPayOrderTimeOutListener);
        subscription = new Subscription();
        subscription.setTopic("invoice");
        subscriptionTable.put(subscription, invoiceMessageListener);
        //订阅多个topic如上面设置
        orderConsumerBean.setSubscriptionTable(subscriptionTable);
        return orderConsumerBean;
    }

}
